<?php

class Ebx_Helper_PrettyDateFormat implements Ebx_Helper_Interface {

	function direct($date, $options = array()) {
		$format = FORMAT_DATE;
		$indernalFormat = CSite::GetDateFormat();

		if (is_string($options)) {
			$format = $options;
		}
		elseif (is_array($options)) {
			if (isset($options['format'])) {
				$format = $options['format'];
			}
			if (isset($options['internal_format'])) {
				$indernalFormat = $options['internal_format'];
			}
		} 

		if ($date != date('U', (int)$date)) {
			$timestamp = MakeTimeStamp($date, $indernalFormat);
		} else {
			$timestamp = $date;
		}
		
		$parseFormat = preg_split("/[^a-zA-Z]+/", $format);

		$dt = array();
		for ($i = 0, $cnt = count($parseFormat); $i < $cnt; $i++) {
			$part = $parseFormat[$i];
			switch ($part) {
				case 'MM':
					$dt[] = date('m', $timestamp);
				break;
					
				case 'M':
					$dt[] = date('n', $timestamp);
				break;

				case 'YYYY':
					$dt[] = date('Y', $timestamp);
				break;

				case 'YY':
					$dt[] = date('y', $timestamp);
				break;

				case 'D':
					$dt[] = date('j', $timestamp);
				break;

				case 'DD':
					$dt[] = date('d', $timestamp);
				break;

				case 'F':
					$dt[] = GetMessage("MONTH_".date('n', $timestamp));
				break;

				case 'Ff':
					$m = GetMessage("MONTH_".date('n', $timestamp));
					$dt[] = ToLower($m);
				break;

				case 'FS':
					$dt[] = GetMessage("MONTH_".date('n', $timestamp).'_S');
				break;

				case 'Fs':
					$m = GetMessage("MONTH_".date('n', $timestamp).'_S');
					$dt[] = ToLower($m);
				break;

				case 'MI':
					$dt[] = date('i', $timestamp);
				break;

				case 'HH':
					$dt[] = date('H', $timestamp);
				break;

				case 'SS':
					$dt[] = date('s', $timestamp);
				break;

				default:
				   $dt[] = date($part, $timestamp);
			}
		}

		//echo '<pre>'; print_r($dt); echo '</pre>';
		
		return str_replace($parseFormat, $dt, $format);

	}

}

?>